using Tools.Domain.Common.Repositories.Contracts;
using Tools.Domain.Specifications.Constraints;
using Tools.Domain.Specifications.Common.Reflection;
using Webshop20.Domain.Customers.Specifications;

namespace Webshop20.Domain.Customers.Core
{
    public class CustomerConstraintSet : ConstraintSetBase<Customer>
    {

        public CustomerConstraintSet(IConstraintContext constraintContext, IRepository<Customer> customerRepository) : base(constraintContext)
        {

 

            AddInvariant(
                
                new StringFieldIsNotNullOrEmpty<Customer>(x => x.FirstName, "No_FIRST_NAME") &
                new StringFieldIsNotNullOrEmpty<Customer>(x => x.LastName, "NO_LAST_NAME") &
                new StringFieldValueIsValidEMailAdress<Customer>(x => x.EMail, "INVALID_EMAIL") &
                new EMailAdressUniqueSpecification<Customer>(customerRepository)
                
                );
            ForMethod(customer => customer.SetCustomerData(null, null, null, null, null, null,null,null))
                .AddPrecondition(
                new StringFieldIsNotNullOrEmpty<SetCustomerDataData>(x => x.FirstName, "No_FIRST_NAME") &
                new StringFieldIsNotNullOrEmpty<SetCustomerDataData>(x => x.LastName, "NO_LAST_NAME") &
                new StringFieldValueIsValidEMailAdress<SetCustomerDataData>(x => x.EMail, "INVALID_EMAIL") 
                )
                .AddPrecondition(new EMailAdressUniqueSpecification<SetCustomerDataData>(customerRepository))
                .AddPostcondition(new ReferenceNotSet<Customer>(customer=>customer.Adress, "NO_ADRESS_SET"), new AlwaysExceptForRegistration() );

        }
    }
}